﻿/// <summary>
/// Author: NPQuoc
/// Date: 2013/06/06
/// </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ivs.Core.Mapping;
using SketchTour.Models;
using System.Collections;
using Ivs.Core.Common.Utilities;
namespace SketchTour.DAL
{
    public class MSRM00DAL
    {
        private EFBaseDao db;
        public MSRM00DAL()
        {
            db = new EFBaseDao();
        }

        #region Add function

        public List<m_function> getAll_Function()
        {
            List<m_function> listFunction = db.m_function.ToList();
            return listFunction;
        }
        //public Boolean check
        public m_roledetail search_RoleDetail(MSRM00Models model)
        {
            m_roledetail _RoleDetail = new m_roledetail();
            List<m_roledetail> _listRoleDetail = new List<m_roledetail>();
            if (!Utilities.isNullOrEmpty(model.RoleID))
            {
                _listRoleDetail = db.m_roledetail.Where(a => a.RoleID == model.RoleID).ToList();
            }
            if (!Utilities.isNullOrEmpty(model.ScreenCode))
            {
                _listRoleDetail = _listRoleDetail.Where(a => a.ScreenCode == model.ScreenCode).ToList();
            }
            if (!Utilities.isNullOrEmpty(model.FunctionID))
            {
                _listRoleDetail = _listRoleDetail.Where(a => a.FunctionID == model.FunctionID).ToList();
            }
            if (_listRoleDetail.Count > 0)
            {
                _RoleDetail = _listRoleDetail.FirstOrDefault();
            }
            else
            {
                _RoleDetail = null;
            }
            return _RoleDetail;
        }

        public m_role search_Role(MSRM00Models model)
        {
            m_role _Role = new m_role();
            if (!Utilities.isNullOrEmpty(model.RoleID))
            {
                _Role = db.m_role.Where(a => a.ID == model.RoleID).FirstOrDefault();
            }
            else
            {
                _Role = null;
            }
            return _Role;
        }
        public string search_ExistRole(MSRM00Models model)
        {
            int iError = 0;
            string _RoleName = "";
            List<m_role> _listRoles = new List<m_role>();
            m_role _Role = new m_role();

            if (!Utilities.isNullOrEmpty(model.RoleName))
            {
                _listRoles = db.m_role.Where(a => a.RoleName == model.RoleName.Trim()).ToList();
            }

            if (_listRoles.Count > 0)
            {
                for (int i = 0; i < _listRoles.Count; i++)
                {
                    int _IdLoop = Utilities.parseInt(_listRoles[i].ID);
                    if (_IdLoop == model.RoleID)
                    {
                        break;
                        iError = 1;
                    }
                    else {
                        _RoleName = _listRoles[i].RoleName;
                    }
                }   
            }
            if (iError == 1)
            {
                return null;
            }
            else {
                return _RoleName;
            }
        }
        public List<m_role> getAll_Role()
        {
            List<m_role> _ListRole = new List<m_role>();
                _ListRole = db.m_role.ToList();
                return _ListRole;
        }
        public m_screen search_Screen(MSRM00Models model)
        {
            m_screen _Screen = new m_screen();
            if (!Utilities.isNullOrEmpty(model))
            {
                _Screen = db.m_screen.Where(a => a.ScreenCode == model.ScreenCode).FirstOrDefault();
            }
            return _Screen;
        }
        public List<m_screen> getAll_Screen()
        {
            List<m_screen> _listScreen = db.m_screen.ToList();
            return _listScreen;
        }
        public List<MSRM00Models> getList_MSRM00Models()
        {
            var screen = from s in db.m_screen
                         select new MSRM00Models
                         {
                             ScreenCode = s.ScreenCode,
                             ScreenName = s.ScreenName,
                             SortOrder = s.SortOrder,
                             MenuLevel = s.MenuLevel,
                             Url = s.Url,
                             ParentMenuID = s.ParentMenuID,
                             ListParentID = s.ListParentID,
                             MultiParentFlag = s.MultiParentFlag,
                             RootFlag = s.RootFlag,
                             HistoryFlag = s.HistoryFlag
                         };
            return screen.ToList();
        }


        public List<MSRM00Models> GetScreenForRoleDetail(int roleId)
        {
            List<MSRM00Models> screenList = getList_MSRM00Models();
            List<m_function> functionList = db.m_function.ToList();

            List<m_roledetail> roleDetailList = db.m_roledetail.Where(a => a.RoleID == roleId).ToList();

            Hashtable htbIndex = ConvertFunctionCodeToIndex(functionList);
            IEnumerable<m_roledetail> list = new List<m_roledetail>();
            int index = 0;
            for (int i = 0; i < screenList.Count; i++)
            {
                screenList[i].ScreenFunction = new bool[functionList.Count];
                list = roleDetailList.Where(x => x.ScreenCode == screenList[i].ScreenCode);
                for (int j = 0; j < list.Count(); j++)
                {
                    index = Convert.ToInt16(htbIndex[list.ElementAt(j).m_function.ID]);
                    screenList[i].ScreenFunction[index] = true;
                }
            }
            return screenList;
        }

        public Hashtable ConvertFunctionCodeToIndex(List<m_function> list)
        {

            Hashtable htbResult = new Hashtable();
            for (int i = 0; i < list.Count; i++)
            {
                htbResult.Add(list[i].ID, i);


            }

            return htbResult;
        }
        #endregion
    }
}